<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增员工</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <style>
      body {margin:20px 20px 0;}
      .layui-form-item--require label::before {
            content: '*';
            position: relative;
            z-index: 10;
            left: -5px;
            font-family: 'simsun', '宋体';
            font-size: 15px;
            color: #f00;
      }
      .form-txt {color:#999; font-size:12px; margin-top:10px; display:block;}
      .layui-form-item .layui-form-label {width:110px;}
      .layui-form-item .layui-input-block {margin-left:140px;}
      .hold {margin-top:10px; width:120px; letter-spacing:5px;}
      #juryDistrict .layui-input-inline {width:150px;}
    </style>
  </head>
  <body>
    <form class="layui-form" autocomplete="off">
      <div class="layui-fluid">
        <div class="layui-row">
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                  <input type="text" name="name" lay-verify="name" placeholder="请输入姓名" class="layui-input" maxlength="10">
                </div>
              </div>
          </div>
          <div class="layui-col-sm6">
            <div class="layui-col-sm10">
              <div class="layui-form-item layui-form-item--require">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-block">
                  <input type="text" name="idNo" lay-verify="idNo" placeholder="请输入身份证号" class="layui-input" maxlength="18">
                </div>
              </div>
            </div>
            <div class="layui-col-sm2" style="padding-left:4px;">
              <input type="text" name="sex" class="layui-input" maxlength="1" disabled>
              <input type="text" name="birthday" id="birthday" class="layui-input" style="display:none;">
            </div>
          </div>
        </div>

        <div class="layui-row">
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
                <label class="layui-form-label">手机号码</label>
                <div class="layui-input-block">
                    <input type="text" name="mobile" lay-verify="mobile" placeholder="请输入手机号码" class="layui-input" maxlength="11">
                </div>
            </div>
          </div>
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-input-block">
                      <select name="departmentId" lay-verify="departmentId" lay-filter="departmentId">
                        <option value="">请选择部门</option>
                      </select>
                    </div>
            </div>
          </div>
        </div>

        <div class="layui-row">
          <div class="layui-col-sm6">
            <div class="layui-form-item layui-form-item--require">
                    <label class="layui-form-label">所属公司</label>
                    <div class="layui-input-block">
                      <select name="companyId" lay-verify="companyId" lay-filter="companyId">
                        <option value="">请选择所属公司</option>
                      </select>
                    </div>
            </div>
          </div>
          <div class="layui-col-sm6">
                <div class="layui-form-item layui-form-item--require">
                    <label class="layui-form-label">数据等级</label>
                    <div class="layui-input-block">
                      <select name="permiCode" lay-verify="permiCode">
                        <option value="1" selected="selected">初级（仅看自己录入的数据）</option>
                        <option value="2">中级（能查看本部门的数据）</option>
                        <option value="3">高级（能看到所在公司的数据）</option>
                      </select>
                    </div>
            </div>
          </div>
        </div>

        <!-- <div class="layui-row">
          <div class="layui-col-sm12">
                <div class="layui-form-item layui-form-item--require">
                    <label class="layui-form-label">数据等级</label>
                    <div class="layui-input-block">
                      <select name="permiCode" lay-verify="permiCode">
                        <option value="1" selected="selected">初级（仅看自己录入的数据）</option>
                        <option value="2">中级（能查看本部门的数据）</option>
                        <option value="3">高级（能看到所在公司的数据）</option>
                      </select>
                    </div>
            </div>
          </div>
        </div> -->

        <input type="hidden" name="id">
        <div class="layui-form-item" style="text-align:center;">
          <button class="layui-btn layui-btn-normal" id="vsubmit" lay-submit lay-filter="submit" style="width:150px;">保存</button>
          <button class="layui-btn layui-btn-primary charge-cancel" type="button" style="width:150px;">取消</button>
        </div>

      </div>
    </form>
  </body>
  <script charset="utf-8" src="/view/frame/layui/layui.js"></script>
  <script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
  <script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>
  <script charset="utf-8" src="/view/frame/static/js/lang_zh_CN.js?v=1.0"></script>
  <script charset="utf-8" src="/view/frame/static/js/layui.district.js?v=1.0"></script>
  <script charset="utf-8" src="/view/tms/static/js/tms_company.js?v=1.0"></script>
  
  <script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
  <script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
  <script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
  <script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>
  
  <script>
  layui.use(['form', 'layer', 'layedit', 'laydate'], function(){
    var form = layui.form,
        layer = layui.layer,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.jquery;

    $(".charge-cancel").click(function(ev) {
        parent.layer.closeAll('iframe'); 
    })

    //自定义验证规则
    form.verify({
      name: function(v){
        if(v.length == 0){
          return '请输入姓名';
        }else if(v.length < 2 || v.length > 10){
          return '姓名长度为2到10位字符';
        }else if(!$jsReg.zhName.test(v)){
          return '姓名格式不正确';
        }
      },
      idNo: function(v){
        if(v.length == 0){
          return '请输入身份证号';
        }else if(!$jsReg.idNo.test(v)){
          return '身份证号格式不正确';
        }
      },
      mobile: function(v){
        if(v.length == 0){
          return '请输入手机号';
        }else if(v.length != 11){
          return '手机号长度为11位';
        }else if(!$jsReg.mobile.test(v)){
          return '手机号格式不正确';
        }
      },
      companyId: function(v){
        if(v.length == 0){
          return '请选择所属公司';
        }
      },
      departmentId: function(v){
        if(v.length == 0){
          return '请选择所属部门';
        }
      },
      permiCode: function(v){
        if(v.length == 0){
          return '请选择数据等级';
        }
      }
    
    });

    //初始化公司部门
    $('body').companyDepartment(form, [], ['companyId', 'departmentId']);

    //身份证失焦后获取性别和出生年月，第17位是性别，奇数男，偶数女，第7到14位是出生年月
    $('input[name="idNo"]').on('blur', function() {
        var $idNo = $.trim($(this).val());
        if ($idNo.length == 18) {
            var $sex = parseInt($idNo.substr(16, 1)) % 2 == 1 ? "男" : "女";
            var $birthday = $idNo.substr(6, 4) + '-' + $idNo.substr(10, 2) + '-' + $idNo.substr(12, 2);
            $('input[name="sex"]').val($sex);
            $('input[name="birthday"]').val($birthday);
        }
    });

    //监听提交
 
      form.on('submit(submit)', function(data){
        var $sex = $.trim(data.field.sex) == '男' ? 0 : 1;
        
        //数据源
        var $saveData = {
          name: $.trim(data.field.name),
          idNo: $.trim(data.field.idNo),
          sex: $sex,
          birthday: $.trim(data.field.birthday),
          mobile: $.trim(data.field.mobile),
          companyId: parseInt($.trim(data.field.companyId)),
          departmentId: parseInt($.trim(data.field.departmentId)),
          permiCode: parseInt($.trim(data.field.permiCode)),
        };

        //保存数据，调用接口
        $.ajax({
          type:'POST', 
          url:'/ucenter/centre/permi/employee/add.shtml',
          dataType:"json",      
          contentType:"application/json",               
          data:JSON.stringify($saveData), 
          success:function(d){
            var $code = d.code,
                $msg = d.msg,
                $objects = d.objects;
                
            if($code === 'SUCCESS'){
              parent.layer.alert('保存成功！', {
                yes: function(){                  
                  //关闭窗口
                  parent.layer.closeAll();
                }
              });
            }else if($code === 'ERROR_PWD_CANNOT_NULL'){
              parent.layer.alert('密码不能为空！');
            }else if($code === 'ERROR_EMPLOYEE_EXIST'){
              parent.layer.alert('身份证已存在！');
            }else if($code === 'ERROR_MOBILE_EXIST'){
              parent.layer.alert('手机号已存在！');
            }else if($code === 'ERROR_USER_EXIST'){
              parent.layer.alert('登录帐号已被使用！');
            }else{
              parent.layer.alert('保存信息失败，请重新填写！');
            }
          }
        });
        return false;
      });
    
  });
  </script>
</html>